var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		keyword: '',
		pagenum: 1,
		datalist: [],
		history_list: [],
		history_show: true,
		order: '',
		groupid: '',
		glist: [],
		clist: [],
		productlisttype: 'itemlist'
	},
	_onLoad: function () {
		var that = this
		if(that.options.keyword){
			that.setData({ keyword: that.options.keyword});
		}
		var productlisttype = $.getCache('productlisttype');
		if (productlisttype) that.setData({ productlisttype: productlisttype })
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
		var cateid = that.data.cateid
		if (!cateid) cateid = that.options.cid
    $.get('shop/search', {cid:cateid}, function (res) {
			delete res.productlisttype
			res.isload = true
      that.setData(res)
			//if(res.datalist.length == 0) {
			//	$.showEmpty('没有搜索到相关商品');
			//}
    })
		if(that.data.keyword){
			that.searchproduct();
		}
		//that.getProductList();
	},
	onShow: function () {
		var that = this;
		that.setData({
			history_list: $.getCache('search_history_list'),
		});
	},
	searchChange: function (e) {
		this.setData({
			keyword: e.detail.value
		});
		if (e.detail.value == '') {
			this.setData({ history_show: true, datalist: [] })
		}
	},
	searchbtn: function () {
		var that = this;
		if (that.data.history_show) {
			var keyword = that.data.keyword;
			if (keyword.length == 0) {
				$.error('请输入您要搜索的商品名'); return;
			}
			that.searchproduct();
		} else {
			if (that.data.productlisttype == 'itemlist') {
				that.setData({ productlisttype: 'item2' })
				$.setCache('productlisttype', 'item2')
			} else {
				that.setData({ productlisttype: 'itemlist' })
				$.setCache('productlisttype', 'itemlist')
			}
		}
	},
	searchConfirm: function (e) {
		var that = this;
		var keyword = e.detail.value;
		if (keyword.length == 0) {
			$.error('请输入您要搜索的商品名'); return;
		}
		that.setData({ keyword: keyword });
		that.searchproduct();
	},
	searchproduct: function () {
		var that = this;
		that.setData({
			pagenum: 1,
			datalist: [],
			$nomoreData: false
		});
		that.addHistory();
		that.getProductList();
	},
	sortClick: function (e) {
		var that = this;
		var t = e.currentTarget.dataset;
		that.setData(t);
		that.searchproduct();
	},
	groupClick: function (e) {
		var that = this;
		var groupid = e.currentTarget.dataset.groupid;
		if (groupid == this.data.groupid) {
			that.setData({ groupid: '' });
		} else {
			that.setData({ groupid: groupid });
		}
		that.searchproduct();
	},
	cateClick: function (e) {
		var that = this;
		var cateid = e.currentTarget.dataset.cateid;
		if (cateid == this.data.cateid) {
			that.setData({ cateid: '' });
		} else {
			that.setData({ cateid: cateid });
		}
		that.searchproduct();
	},
	filterClick: function () {
		this.setData({ showfilter: !this.data.showfilter })
	},
	getProductList: function () {
		var that = this
		var pagenum = that.data.pagenum
		var keyword = that.data.keyword
		var order = that.data.order
		var field = that.data.field
		var groupid = that.data.groupid
		var cateid = that.data.cateid
		if (!cateid) cateid = that.options.cid
		that.setData({ history_show: false })
		$.post('shop/prolist', { pagenum: pagenum, keyword: keyword, field: field, order: order, groupid: groupid, cid: cateid }, function (res) {
			var data = res.data
			if (pagenum == 1) {
				that.setData({ datalist: data });
				if (data.length == 0) {
					$.showEmpty('没有搜索到相关商品');
				}
			} else {
				if (data.length == 0) {
					//$.error('没有更多数据了');
					that.setData({ $nomoreData: true });
				} else {
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
					that.setData({ datalist: newdata });
				}
			}
		});
	},
	addHistory: function () {
		var that = this
		var keyword = that.data.keyword
		if ($.isNull(keyword)) return;
		var historylist = $.getCache('search_history_list');
		if ($.isNull(historylist)) historylist = [];
		historylist.unshift(keyword);
		var newhistorylist = [];
		for (var i in historylist) {
			if (historylist[i] != keyword || i == 0) {
				newhistorylist.push(historylist[i]);
			}
		}
		if (newhistorylist.length > 5) newhistorylist.splice(5, 1);
		$.setCache('search_history_list', newhistorylist);
		that.setData({ history_list: newhistorylist })
	},
	onReachBottom: function () {
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getProductList();
		}
	},
	historyClick: function (e) {
		var that = this;
		var keyword = e.currentTarget.dataset.value;
		if (keyword.length == 0) return;
		that.setData({
			keyword: keyword
		});
		that.searchproduct();
	},
	deleteSearchHistory: function () {
		var that = this;
		that.setData({
			history_list: null,
		});
		$.removeCache("search_history_list");
	},

}));